<template>
	<view class="saler">
		<view class="topData">
			<view class="topitem" @click="areashow = true">
				<view class="">
					{{region == "" ? address : region}}
				</view>
				<image class="" src="@/static/image/topicon.png" style="width: 32rpx;height:32rpx;" />
			</view>

			<view class="topitem" @click="show = true">
				<view class="">
					{{date == "" ? dataPolice : date}}
				</view>
				<image class="" src="@/static/image/topicon.png" style="width: 32rpx;height:32rpx;" />
			</view>

			<view class="topitem">
				{{num}}单
			</view>

			<view class="topitem">
				{{priceSum}}元
			</view>
		</view>

		<view v-if="dataList.length>0" class="dataList">
			<view class="dataitem" v-for="(item,index) in dataList">
				<view class="subitemtop">
					<view class="time">
						{{item.time}}
					</view>
					<view class="message">
						<view class="">
							单数{{item.data.length}}
						</view>
						<view class="" style="margin-left: 26rpx;">
							总价￥{{item.dataSum}}
						</view>
					</view>
				</view>

				<view class="contentBox" v-for="(subitem,subindex) in item.data">
					<view class="" style="font-size: 25rpx;font-weight: 600;margin-bottom: 10rpx;">
						手机号：{{subitem.mobile}}
					</view>
					<view class="innerBox">
						<view class="info">
							<view class="avatar">
								<image class="" src="@/static/image/driver.png"
									style="width: 45.55rpx;height: 45.55rpx;" />
							</view>
							<view class="nikename">
								{{subitem.name}}
							</view>
						</view>

						<view class="message">
							<view class="name">
								{{subitem.good.name}}
							</view>
							<view class="shuliang">
							</view>
						</view>
						<view class="sumandnum">
							<view class="sum">
								￥{{subitem.good.price}}
							</view>
							<view class="num" style="color: #A7A7A7;">
								x{{subitem.count}}
							</view>
						</view>

					</view>
					<view class="" style="font-size: 25rpx;font-weight: 600;margin-bottom: 10rpx;">
						收货地址：{{subitem.town}}{{subitem.village}}
					</view>

				</view>
			</view>
		</view>

		<view v-else class="">
			<u-empty text="暂无订单" icon="http://cdn.uviewui.com/uview/empty/order.png">
			</u-empty>
		</view>

		<u-picker :show="areashow" ref="uPicker" :columns="columns" @confirm="confirm" @change="changeHandler"
			@cancel="show = false"></u-picker>

		<u-datetime-picker :show="show" v-model="data" mode="date" @confirm="confirmTime"></u-datetime-picker>

	</view>
</template>

<script>
	import {
		salerList,
		getregion
	} from "@/api/api.js"
	export default {
		data() {
			return {
				pageSize: 10,
				page: 1,
				data: "",
				dataPolice: "请选择日期",
				date: "",
				address: "请选择地区",
				num: 0,
				sum: 0,
				dataList: [],
				show: false,
				areashow: false,
				columns: [
					[],
					[],
					[],
					[],
					[]
				],
				province: [], //省
				sheng: [],
				city: [], //市
				shi: [],
				area: [], //区县
				qu: [],
				township: [], //乡镇
				xiangzhen: [],
				hamlet: [], //村庄
				cunzhuang: [],
				shengCode: null, //省id
				shiCode: null, //市id
				quCode: null, //区id
				xiangzhenCode: null, //乡镇id
				cunzhuangCode: null, //村庄code
				level: "", //地区级别1-5省市县镇村
				region: "", //地区
				priceSum: 0

			}
		},
		onLoad() {
			this.getsalerList()
			this.handleGetregion()
		},
		methods: {
			async getsalerList() {
				let data = []
				this.priceSum = 0
				let res = await salerList({
					page_size: this.pageSize,
					page: this.page,
					date: this.data,
					region: this.region,
					date: this.date,
				})
				for (let i in res.data.return.data) {
					let sum = 0
					res.data.return.data[i].forEach((item, index) => {
						item.datatime = i
					})
					data.push({
						time: i,
						data: res.data.return.data[i],
						sum: sum
					})
					data.sort(function(a, b) {
						return Date.parse(b.time.replace(/-/g, "/")) - Date.parse(a.time.replace(
							/-/g, "/"));
					});
				}

				this.dataList = data
				this.dataList.forEach((item, index) => {
					let dataSum = 0
					item.data.forEach((subitem, subindex) => {
						dataSum += (subitem.good.price * subitem.count)
					})
					item.dataSum = dataSum
					this.priceSum += dataSum
				})
				this.num = this.dataList.length
				console.log("数据看板", this.dataList)

			},

			confirmTime(e) {
				console.log("eeeee", e)
				this.date = this.shijianc(e.value)
				this.show = false
				this.dataList = []
				this.getsalerList()
			},
			shijianc(time) {
				let date = new Date(time)
				let Y = date.getFullYear() + '-'
				let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
				let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
				return Y + M + D
			},

			async handleGetregion() {
				let res = await getregion()
				console.log("省市区", res)
				res.data.forEach((item, index) => {
					this.province.push(item.name)
					this.sheng.push(item.code)
				})
				this.columns[0] = this.province
			},

			async handleGetregion() {
				let res = await getregion()
				console.log("省市区", res)
				res.data.forEach((item, index) => {
					this.province.push(item.name)
					this.sheng.push(item.code)
				})
				this.columns[0] = this.province
			},

			async changeHandler(e) {
				// console.log("eeee", e)
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					picker = this.$refs.uPicker
				} = e

				//获取市
				if (columnIndex === 0) {
					this.city = []
					this.shi = []

					this.shengCode = this.sheng[index]
					uni.showToast({
						title: this.province[index],
						icon: "none",
						duration: 1500
					});
					// console.log("shengCode", shengCode)
					let res = await getregion({
						parent_code: this.shengCode
					})
					// console.log("市", res)
					res.data.forEach((item, index) => {
						this.city.push(item.name)
						this.shi.push(item.code)
					})
					this.columns[1] = this.city
					this.columns[2] = []
					this.columns[3] = []
					this.columns[4] = []
				}

				//获取区
				if (columnIndex === 1) {
					this.area = []
					this.qu = []

					this.shiCode = this.shi[index]
					uni.showToast({
						title: this.city[index],
						icon: "none",
						duration: 1500
					});
					let res = await getregion({
						parent_code: this.shiCode
					})
					res.data.forEach((item, index) => {
						this.area.push(item.name)
						this.qu.push(item.code)
					})
					this.columns[2] = this.area
					this.columns[3] = []
					this.columns[4] = []
				}

				//乡镇
				if (columnIndex === 2) {
					this.township = []
					this.xiangzhen = []
					this.quCode = this.qu[index]
					uni.showToast({
						title: this.area[index],
						icon: "none",
						duration: 1500
					});
					let res = await getregion({
						parent_code: this.quCode
					})
					res.data.forEach((item, index) => {
						this.township.push(item.name)
						this.xiangzhen.push(item.code)
					})
					this.columns[3] = this.township
					this.columns[4] = []
				}

				//村庄
				if (columnIndex === 3) {
					this.hamlet = []
					this.cunzhuang = []
					this.xiangzhenCode = this.xiangzhen[index]
					uni.showToast({
						title: this.township[index],
						icon: "none",
						duration: 1500
					});
					let res = await getregion({
						parent_code: this.xiangzhenCode
					})
					res.data.forEach((item, index) => {
						this.hamlet.push(item.name)
						this.cunzhuang.push(item.code)
					})
					this.columns[4] = this.hamlet
				}


				if (columnIndex === 4) {
					this.cunzhuangCode = this.cunzhuang[index]
					uni.showToast({
						title: this.hamlet[index],
						icon: "none",
						duration: 1500
					});
					// console.log("55555555")
				}
			},
			confirm(e) {
				console.log("e", e)
				this.address = ""
				e.value.forEach((item, index) => {
					if (item != undefined) {
						this.address += item
						this.level = index + 1
						this.region = item
					}
				})
				console.log("address", this.region, this.level)
				this.areashow = false
				this.dataList = []
				this.getsalerList()
			},


		}
	}
</script>

<style scoped lang="scss">
	.saler {
		.topData {
			width: 100%;
			height: 120rpx;
			background: linear-gradient(132deg, rgba(255, 158, 69, 0.94) 0%, rgba(244, 62, 62, 0.79) 100%);
			border-radius: 32rpx 32rpx 0rpx 0rpx;
			opacity: 1;
			margin-top: 32rpx;
			display: flex;
			padding: 34rpx 60rpx;
			box-sizing: border-box;
			justify-content: space-between;

			.topitem {
				font-size: 28rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FFFFFF;
				line-height: 50rpx;
				display: flex;
				align-items: center;
			}
		}

		.dataList {
			width: 100%;
			padding: 24rpx;
			box-sizing: border-box;

			.dataitem {
				width: 100%;

				.subitemtop {
					width: 100%;
					display: flex;
					justify-content: space-between;
					padding: 20rpx;
					box-sizing: border-box;

					.time {
						font-size: 28rpx;
						font-family: PingFang SC-Semibold, PingFang SC;
						font-weight: 600;
						color: #000000;
						line-height: 50rpx;
					}

					.message {
						display: flex;
						font-size: 24rpx;
						font-family: PingFang SC-Semibold, PingFang SC;
						font-weight: 600;
						color: #A7A7A7;
						line-height: 50rpx;
					}

				}

				.contentBox {
					width: 100%;
					background: #FFFFFF;
					box-shadow: 0rpx 0rpx 8rpx 2rpx rgba(0, 0, 0, 0.05);
					border-radius: 16rpx 16rpx 16rpx 16rpx;
					padding: 18rpx 16rpx;
					box-sizing: border-box;
					margin-bottom: 16rpx;
					margin-top: 10rpx;

					.innerBox {
						display: flex;
						justify-content: space-between;

						.info {
							display: flex;
							font-size: 28rpx;
							font-family: PingFang SC-Semibold, PingFang SC;
							font-weight: 600;
							color: #000000;
							line-height: 50rpx;
						}

						.message {
							.name {
								width: 387rpx;
								height: 50rpx;
								font-size: 24rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #000000;
								line-height: 50rpx;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}

							.shuliang {
								font-size: 24rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #A7A7A7;
								line-height: 50rpx;
							}

						}

						.sumandnum {
							font-size: 24rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #000000;
							line-height: 50rpx;
							text-align: right;
						}
					}

					.dfk {
						font-size: 28rpx;
						font-family: PingFang SC-Semibold, PingFang SC;
						font-weight: 600;
						color: #000000;
						line-height: 50rpx;
						text-align: right;
					}
				}

			}
		}
	}
</style>